package AFMAnalysis;

import java.util.ArrayList;

public class Line{

	double slope;
	double r;
	int blobID;
	ArrayList<Pixel> pixels;

	/**
	 * This is the constructor for this class. The default r value is 1.
	 * 
	 */
	
	public Line(){

		pixels = new ArrayList<Pixel>();
		r = 1;
	}

	/**
	 * This method computes the regression of the Pixels in the line. The slope and
	 * r value of the regression are retained for further analysis.
	 * 
	 */
	
	public void regression(){


		int snum = pixels.size()*sumxy() - sumx()*sumy();
		int sden = pixels.size()*sumx2() - sumx()*sumx();

		if(sden != 0){
			slope = (double)snum/sden;
		}
		else{
			slope = 10000;
		}

		int rnum = pixels.size()*sumxy() - sumx()*sumy();
		double rden = (double)(pixels.size()*sumx2() - sumx()*sumx())*(double)(pixels.size()*sumy2() - sumy()*sumy());

		r = (double)rnum*rnum/rden;
		
		return;
	}

	/**
	 * This method sums up the multiplication of each Pixel's x and y values.
	 * 
	 * @return The desired sum
	 */
	
	public int sumxy(){

		int sum = 0;

		for(int count = 0; count < pixels.size(); count++){

			sum = sum + pixels.get(count).x*pixels.get(count).y;
		}

		return sum;
	}
	
	/**
	 * This method sums up each Pixel's x value.
	 * 
	 * @return The desired sum
	 */

	public int sumx(){

		int sum = 0;

		for(int count = 0; count < pixels.size(); count++){

			sum = sum + pixels.get(count).x;
		}

		return sum;
	}

	/**
	 * This method sums up each Pixel's y value.
	 * 
	 * @return The desired sum
	 */
	
	public int sumy(){

		int sum = 0;

		for(int count = 0; count < pixels.size(); count++){

			sum = sum + pixels.get(count).y;
		}

		return sum;
	}

	/**
	 * This method sums up each Pixel's x value squared.
	 * 
	 * @return The desired sum
	 */
	
	public int sumx2(){

		int sum = 0;

		for(int count = 0; count < pixels.size(); count++){

			sum = sum + pixels.get(count).x*pixels.get(count).x;
		}

		return sum;
	}

	/**
	 * This method sums up each Pixel's y value squared.
	 * 
	 * @return The desired sum
	 */
	
	public int sumy2(){

		int sum = 0;

		for(int count = 0; count < pixels.size(); count++){

			sum = sum + pixels.get(count).y*pixels.get(count).y;
		}

		return sum;
	}
	
	/**
	 * This main function is a test function to see if the regression code is 
	 * working properly. It adds a few sample points and computes the line for them.
	 * 
	 * @param args
	 */
	
	public static void main(String[] args){
		
		Pixel[] pix = new Pixel[6];
		pix[0] = new Pixel(false, false, -1, null, null, 1, 1);
		pix[1] = new Pixel(false, false, -1, null, null, 2, 1);
		pix[2] = new Pixel(false, false, -1, null, null, 3, 1);
		pix[3] = new Pixel(false, false, -1, null, null, 3, 2);
		pix[4] = new Pixel(false, false, -1, null, null, 0, 1);
		pix[5] = new Pixel(false, false, -1, null, null, -1, 1);
		
//		pix[0] = new Pixel(false, false, -1, null, null, 1, 1);
//		pix[1] = new Pixel(false, false, -1, null, null, 1, 2);
//		pix[2] = new Pixel(false, false, -1, null, null, 1, 3);
//		pix[3] = new Pixel(false, false, -1, null, null, 2, 3);
//		pix[4] = new Pixel(false, false, -1, null, null, 1, 0);
//		pix[5] = new Pixel(false, false, -1, null, null, 1, -1);
		
		Line line = new Line();
		line.pixels.add(pix[0]);
		line.pixels.add(pix[1]);
		line.pixels.add(pix[2]);
		line.pixels.add(pix[3]);
		line.pixels.add(pix[4]);
		line.pixels.add(pix[5]);
		
		line.regression();
		
		System.out.println(line.slope + ", " + line.r);
	}

}